﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.Models.Hethong;
using ChineseSchool.BLL;
using System.Data;

namespace ChineseSchool.Admin
{
    public partial class QL_News : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BinGrid();
                btnAdd.Visible = true;
                btnEdit.Visible = true;
            }
        }

        protected void BinGrid()
        {
            var bNews = new B_News();
            var lstMNews = bNews.GetAll();
            grdNews.DataSource = lstMNews;
            grdNews.DataBind();
        }

        protected void Bin_ddlNewsType()
        {
            var dtMenu = (new B_NewsType()).GetAll();
            ddlNewsType.DataSource = dtMenu;
            ddlNewsType.DataValueField = "NewsTypeID";
            ddlNewsType.DataTextField = "NewsTypeName";
            ddlNewsType.DataBind();
        }

        protected void grdNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdNews.PageIndex = e.NewPageIndex;
            BinGrid();
        }

        protected void grdNews_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var newsId = Convert.ToInt32(e.CommandArgument);
            var mNews = new M_News();
            var bNews = new B_News();
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            if (e.CommandName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    bInsertUpdateDelete.delete("News", "NewsID", mNews, newsId);
                    Response.Write("<script>alert('Đã được xóa!')</script>");
                }
                catch (Exception ex)
                {
                    throw new Exception("Đã có lỗi xảy ra! Vui lòng thử lại lần sau!", ex);
                }
                BinGrid();
            }
            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                panAdd.Visible = true;
                btnAdd.Visible = false;
                lblTitle.Text = "Cập nhật";
                Bin_ddlNewsType();
                mNews = bNews.Selectby_id(newsId);
                var newsTypeId = Convert.ToInt32(mNews.newsTypeID.ToString());
                if (newsTypeId == 0)
                {
                }
                {
                    ddlNewsType.SelectedValue = newsTypeId.ToString();
                }
                txtTitle.Text = mNews.title;
                txtSummary.Value = mNews.summary;
                ckNewsContent.Value = mNews.newsContent;
                imgDisplay.ImageUrl = mNews.imageUrl;
                if (mNews.isHot.ToString() == "True")
                {
                    rbYes.Checked = true;
                }
                else
                {
                    rbNo.Checked = true;
                }
                btnEdit.CommandArgument = newsId.ToString();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            panAdd.Visible = true;
            btnEdit.Visible = false;
            lblTitle.Text = "Thêm mới";
            txtTitle.Text = "";
            txtSummary.Value = "";
            ckNewsContent.Value = "";
            rbNo.Checked = true;
            Bin_ddlNewsType();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnEdit.Visible = true;
            /*tải ảnh */
            var imageUrl = "";
            var file2 = fuImageUrl.PostedFile;
            if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
            {
                lblThongbao.Text = "File nặng quá T_T";
            }
            else
            {
                try
                {
                    var path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                    fuImageUrl.SaveAs(path2);
                    imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                    lblThongbao.Text = "Tải thành công";
                }
                catch
                {
                    lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                }
            }

            /*tải ảnh */
            var mNews = new M_News();
            mNews.title = txtTitle.Text;
            var newsTypeId = 0;
            if (ddlNewsType.SelectedValue != "")
            {
                newsTypeId = Convert.ToInt32(ddlNewsType.SelectedValue);
            }
            mNews.newsTypeID = newsTypeId;
            mNews.summary = txtSummary.Value;
            mNews.imageUrl = imageUrl;
            const string format = "ddd, dd/MM/yyyy h:mm:ss tt";
            mNews.postDate = DateTime.ParseExact(DateTime.Now.ToString(format), "ddd, dd/MM/yyyy h:mm:ss tt", null);
            mNews.newsContent = ckNewsContent.Value;
            mNews.isHot = rbYes.Checked;
            mNews.isLock = false;
            bInsertUpdateDelete.insert("News", "NewsID", mNews);
            panAdd.Visible = false;
            BinGrid();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnAdd.Visible = true;
            var imageUrl = "";
            var mNews = new M_News();
            var bNews = new B_News();
            var newsId = Convert.ToInt32(btnEdit.CommandArgument);
            var file2 = fuImageUrl.PostedFile;
            if (fuImageUrl.HasFile)
            {
                if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
                {
                    lblThongbao.Text = "File nặng quá T_T";
                }
                else
                {
                    try
                    {
                        string path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                        fuImageUrl.SaveAs(path2);
                        imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                        lblThongbao.Text = "Tải thành công";
                    }
                    catch
                    {
                        lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                    }
                }
            }
            else
            {
                mNews = bNews.Selectby_id(newsId);
                imageUrl = mNews.imageUrl;
            }
            /*tải ảnh */
            mNews.newsID = Convert.ToInt32(btnEdit.CommandArgument);
            mNews.title = txtTitle.Text;
            var newsTypeId = 0;
            if (ddlNewsType.SelectedValue != "")
            {
                newsTypeId = Convert.ToInt32(ddlNewsType.SelectedValue);
            }
            mNews.newsTypeID = newsTypeId;
            mNews.newsContent = ckNewsContent.Value;
            mNews.summary = txtSummary.Value;
            mNews.imageUrl = imageUrl;
            const string format = "ddd, dd/MM/yyyy h:mm:ss tt";
            mNews.postDate = DateTime.ParseExact(DateTime.Now.ToString(format), "ddd, dd/MM/yyyy h:mm:ss tt", null);
            mNews.newsContent = ckNewsContent.Value;
            mNews.isHot = rbYes.Checked;
            mNews.isLock = false;
            bInsertUpdateDelete.update("News", "NewsID", mNews, newsId);
            panAdd.Visible = false;
            BinGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            txtTitle.Text = "";
            txtSummary.Value = "";
            rbNo.Checked = true;
            ckNewsContent.Value = "";
            BinGrid();
        }

        protected void grdNews_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {            
            BinGrid();
        }

        protected void grdNews_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdNews.EditIndex = e.NewEditIndex;
            BinGrid();
        }
    }
}